﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.DBManager;
using DelonixRegiaHotel.Classes;

namespace DelonixRegiaHotel
{
    public partial class GetDuty : Form
    {
        string tempid = "00 - No Duty";
        ArrayList dutyList = new ArrayList();
        ArrayList dutyname = new ArrayList();

        public GetDuty()
        {
            InitializeComponent();
        }

        public bool DutyExist()
        {
            dutyList = StaffAndDutyDBManager.GetAllDutyInfo();
            if (dutyList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRetrieveDuty_Click(object sender, EventArgs e)
        {
            if (DutyExist() == true)
            {
                dutyList = StaffAndDutyDBManager.GetAllDutyInfo();
                dataGridView1.DataSource = dutyList;
                DataGridViewColumn dutyname = dataGridView1.Columns[1];
                DataGridViewColumn dutydesc = dataGridView1.Columns[2];
                dutydesc.Width = 259;
                dutyname.Width = 150;
                
            }
            else
            {
                MessageBox.Show("There are currently no duties");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
                tbxDutyTypeId.Text = row.Cells["dutyTypeID"].Value.ToString();
                tbxDutyName.Text = row.Cells["dutyName"].Value.ToString();
                tbxDutyDesc.Text = row.Cells["dutyDescription"].Value.ToString();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tbxDutyName.Text.Equals("") || tbxDutyDesc.Text.Equals(""))
            {
                MessageBox.Show("All forms have to be filled in");
            }
            
            else
            {
                Duty updateDuty = new Duty();
                updateDuty.DutyTypeID = tbxDutyTypeId.Text;
                updateDuty.DutyName = tbxDutyName.Text;
                updateDuty.DutyDescription = tbxDutyDesc.Text;
        
                if(StaffAndDutyDBManager.GetDutyByName(updateDuty) == true) 
                {
                    MessageBox.Show("Duty already exist");
                    return; // force stop this method. if no return, it will continue running the code below.
                }

                if (StaffAndDutyDBManager.UpdateDuty(updateDuty) == 1)
                {
                    MessageBox.Show("Update is successful");
                    RefreshForm();
                }
            }               
          
        }

        //reload form after update
        public void RefreshForm()
        {
            tbxDutyTypeId.ResetText();
            tbxDutyName.ResetText();
            tbxDutyDesc.ResetText();

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (StaffAndDutyDBManager.DeleteDuty(tbxDutyTypeId.Text) == 1)
            {
                MessageBox.Show("Deleted Sucessfully");
            }
            else
            {
                MessageBox.Show("Duty not deleted, please check");
            }
        }
    



    }
}
